package com.yuqian.JavaSe.leetcode.easy;
/*
 * Given an array and a value, remove all instances of that value in place and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.
从数组中去除某个值，返回新数组的长度；注意在去除完毕后数组中的元素应该变化了，即出现value的位置都被替换掉了，
最后一句的意思是 新长度后面的元素值任意，也就是说，新数组的总长度可以保持不变，
但是新长度之前的元素要正确，后面的无所谓
 */
public class RemoveElement {
	 public static int removeElement(int[] nums, int val) {
	        int start = 0;
	        int count=0;
	        while(count<nums.length){
	        	if(nums[count]!=val){
	        		if(count!=start){
	        			nums[start]=nums[count];
	        		}
	        		count++;
	        		start++;
	        	}else{
	        		count++;
	        	}
	        }
	        for(int i=start;i<nums.length;i++){
	        	nums[i]=0;
	        }
	        return start;
	    }
	 
	 /**
	  * int m = 0;    
   for(int i = 0; i < A.length; i++){
       
       if(A[i] != elem){
           A[m] = A[i];
           m++;
       }
   }
   
   return m;  代码简化版本
	  * @param args
	  */
	 public static void main(String[] args) {
		int[] nums={2,3,3,4,3,5,3,5,6,6,7,8};
		int val = 3;
		int k= removeElement(nums,val);
		System.out.println("K:"+k);
		for(int n:nums){
			System.out.print(n+", ");
		}
		System.out.println(null==null);
	}
}
